<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-4-21
 * Time: 下午3:10
 */
function categories($param = array()){
	$param['catid'] = (is_null($param['catid']))?0:$param['catid'];
	$param['item'] = (is_null($param['item']))?0:$param['item'];
	$flag = '&nbsp;';
	for($i=0; $i<$param['item']; $i++){
		$flag .= '&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	$flag .= '+--';
	$query = DB::query("SELECT * FROM ".DB::table('plugin_iexam_category')." WHERE upid = ".$param['catid']." ORDER BY displayorder ASC");
	while($field = DB::fetch($query)){
		$count = DB::result_first("SELECT COUNT(*) FROM ".DB::table('plugin_iexam_category')." WHERE upid = ".$field['catid']);
		$order = DB::result_first("SELECT MAX(displayorder) FROM ".DB::table('plugin_iexam')." WHERE catid = ".$field['catid']);
		$return['select'][] = array(
			'catid'     => $field['catid'],
			'catname'   => $flag.$field['catname'],
			'order'     => (is_null($order))?0:$order+1
		);
		$return['nodes'][] = array_merge($field, array('open' => true));
		if($count>0){
			$new = array('catid' => $field['catid'], 'item' => ($param['item']+1));
			$return = array_merge_recursive($return, categories($new));
		}
	}
	return $return;
}